﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.DAL.LocalDataAccess/WorkingInfoAccess
* 类       名: WorkingInfoAccess
* 功      能:  TD
* 时      间:  2024/1/4 16:22:21
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.Extensions;

namespace XrayDirection.DAL.LocalDataAccess
{
    public class WorkingInfoAccess
    {

        public static DataResult UpdateWorkingInfo(WorkingInfoEntity workingInfoNew)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    // 查询要更新的数据是否存在
                    var existingWorkingInfo = context.WorkingInfos.FirstOrDefault(wi => wi.Id == workingInfoNew.Id);
                    if (existingWorkingInfo != null)
                    {
                        // 更新实体的属性
                        context.Entry(existingWorkingInfo).CurrentValues.SetValues(workingInfoNew);
                        int affectedRows = context.SaveChanges();
                        if (affectedRows > 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = ConstantManager.DatabaseUpdateError;
                        }
                    }
                    else
                    {
                        result.Message = $"更新失败,要更新的数据不存在,更新的数据Id = {workingInfoNew.Id}";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 虽然WorkingInfo在数据库中只有一条数据,但是还是要用复数,主要是为了防止程序中要在DataGrid中使用.
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<WorkingInfoEntity>> GetWorkingInfos()
        {
            DataResult<List<WorkingInfoEntity>> result = new DataResult<List<WorkingInfoEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.WorkingInfos.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult AddWorkingInfo(WorkingInfoEntity workinginfo)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.Add(workinginfo);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
